package org.hong.monkey.scheduler

import java.util.concurrent.atomic.AtomicBoolean

import org.hong.monkey.util.AsynchronousListenerBus

private[monkey] class LiveListenerBus
  extends AsynchronousListenerBus[MonkeyListener, MonkeyListenerEvent]("MonkeyListenerBus")
  with MonkeyListenerBus {

  private val logDroppedEvent = new AtomicBoolean(false)

  override def onDropEvent(event: MonkeyListenerEvent): Unit = {
    if (logDroppedEvent.compareAndSet(false, true)) {
      // Only log the following message once to avoid duplicated annoying logs.
      logError("Dropping MonkeyListenerEvent because no remaining room in event queue. " +
        "This likely means one of the MonkeyListeners is too slow and cannot keep up with " +
        "the rate at which tasks are being started by the scheduler.")
    }
  }

}
